//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2023-05-31  18-21-04 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2023-05-31  18-21-04 
//===============================





//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2023-05-31  17-04-46 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2023-05-31  17-04-46 
//===============================





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZS.U3D.BASE.Runtime.Singleton;
using ZS.U3D.BASE.Runtime.Event;
using System.Reflection;
using ZS.U3D.BASE.Runtime.StateMachine;

public class GameStart : MonoBehaviour
{
    private StateMachine stateMachine;
    private void Start()
    {
        SingletonManager.Initialize();

        EventManager.Initalize();

        EventManager.AddListener<Event1>(EventHandle);
        EventManager.AddListener<Event2>(EventHandle);
        EventManager.AddListener<Event3>(EventHandle);


        Event2 event1 = new Event2();
        event1.FireEvent();


        stateMachine = new StateMachine(this);
        stateMachine.AddNode<StateIdle>();
        stateMachine.AddNode<StateWalk>();
        stateMachine.AddNode<StateRun>();

        stateMachine.Run<StateIdle>();

    }

    private void Update()
    {
        if (stateMachine!=null)
        {
            stateMachine.Update();
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            stateMachine.Run<StateRun>();
        }
    }

    public void EventHandle(IEvent message) 
    {
        Debug.Log(message);
        if (message.GetType() == typeof(Event1))
        {
            MyDebug.Log("这是 事件1");
        }
        else if (message.GetType() == typeof(Event2))
        {
            MyDebug.Log("这是 事件2");
        }
        else if (message.GetType() == typeof(Event3))
        {
            MyDebug.Log("这是 事件3");
        }
    }

}

public class Event1 : CommonEvent
{
    
}

public class Event2  : CommonEvent
{

}

public class Event3 : CommonEvent
{

}


public class StateIdle : IStateNode
{
    public void OnCreate(StateMachine machine)
    {
        Debug.Log($"{GetType().Name} OnCreate {machine}");
    }

    public void OnCreate(StateMachine machine, params object[] ps)
    {
        throw new System.NotImplementedException();
    }

    public void OnEnd()
    {
        Debug.Log($"{GetType().Name} OnEnd");
    }

    public void OnStart()
    {
        Debug.Log($"{GetType().Name} OnStart");
    }

    public void OnUpdate()
    {
        Debug.Log($"{GetType().Name} OnUpdate");
    }

}

public class StateWalk : IStateNode
{

    public void OnCreate(StateMachine machine, params object[] ps)
    {
        Debug.Log($"{GetType().Name} OnCreate {machine}");
    }

    public void OnEnd()
    {
        Debug.Log($"{GetType().Name} OnEnd");
    }

    public void OnStart()
    {
        Debug.Log($"{GetType().Name} OnStart");
    }

    public void OnUpdate()
    {
        Debug.Log($"{GetType().Name} OnUpdate");
    }
}

public class StateRun : IStateNode
{
    public void OnCreate(StateMachine machine)
    {
        Debug.Log($"{GetType().Name} OnCreate {machine}");
    }

    public void OnCreate(StateMachine machine, params object[] ps)
    {
        throw new System.NotImplementedException();
    }

    public void OnEnd()
    {
        Debug.Log($"{GetType().Name} OnEnd");
    }

    public void OnStart()
    {
        Debug.Log($"{GetType().Name} OnStart");
    }

    public void OnUpdate()
    {
        Debug.Log($"{GetType().Name} OnUpdate");
    }
}


public class PlayerStateMachine 
{

    private StateMachine _playerStateMachine;
    private static bool _isInitialize = false;


    public void Initialize() {
        if (_isInitialize)
        {
            MyDebug.Log("请不要重复初始化");
            return;
        }
        {
            _isInitialize = true;
            _playerStateMachine = new StateMachine(this);
        }
    }

}




